<?php
defined('__JF')
		or die('Direct access is not allowed');
/*
 * Created on 30.11.2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include_once 'JF_ClassNotFoundException.php';
include_once 'JF_ClasstypeException.php';
include_once '../filesystem/JF_FileUtil.php';
final class JF_ReflectionUtil {

	/**
	 * @throws JF_IOException, JF_ClasstypeException, JF_ClassNotFoundException
	 */
	public static function newInstance($sClasspath, array $aParams = array(), $sInterface = null) {
		JF_FileUtil::load($sClasspath);
		$sClassname = self::getClassname($sClasspath);
		if (class_exists($sClassname)) {
			$oRCObject = new ReflectionClass($sClassname);
			if (empty($sInterface) || $oRCObject->implementsInterface($sInterface)) {
				if (empty($aParams)) {
					return $oRCObject->newInstance();
				}
				else {
					return $oRCObject->newInstanceArgs($aParams);
				}

			}
			else {
				throw new JF_ClasstypeException('Class ' . $sClassname . ' not implements ' . $sInterface);
			}

		}
		else {
			throw new JF_ClassNotFoundException('Class not found ' . $sClassname);
		}

	}

	private static function getClassname($sClasspath) {
		$sFilename = basename($sClasspath);
		return str_replace('.php', '', $sFilename);
	}

}